// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-20 11:22
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.DataStruct.ArraysList;

class NumArray {
    private final int[] preSum;

    /**
     * 自定义数组类，使用前缀和计算数组中区间数据和，包含左右边界
     *
     * @param nums:给定数组数据
     */
    public NumArray(int[] nums) {
        preSum = new int[nums.length];
        preSum[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            preSum[i] = preSum[i - 1] + nums[i];
        }
    }

    public int sumRange(int left, int right) {
        if (left == 0) {
            return preSum[right];
        } else {
            return preSum[right] - preSum[left - 1];
        }
    }
}